package com.sunwayhorizo.health.hl7.rim;

import java.io.Serializable;
import java.sql.Date;
import java.sql.Timestamp;

import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.NamedQuery;


import com.sunwayhorizo.health.hl7.common.DataTypeUtil;
import com.sunwayhorizo.health.hl7.common.Hl7Exception;
import com.sunwayhorizo.health.hl7.datatypes.CE;
import com.sunwayhorizo.health.hl7.datatypes.INT;
import com.sunwayhorizo.health.hl7.datatypes.ST;
import com.sunwayhorizo.health.hl7.datatypes.TS;
import com.sunwayhorizo.health.hl7.persistent.RimCoreActCdSets;
import static com.sunwayhorizo.health.hl7.vocabulary.ActCdSetCode.*;


@Entity
@NamedQuery(name = "Document.findAll", query = "select o from Document o")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorValue("DOC")
public class Document extends ContextStructure implements Serializable {
    
/*    public CE completionCode;
    public TS copyTime;
    public II setId;
    public CE storageCode;
    public INT versionNumber;
    public ST medRecordState
*/    
    public Document() throws Hl7Exception {
    }
    
    
    public void setCompletionCode(CE ce) 
    {
    	if(null != ce && ce.codeSystemEnName()!=null && ce.codeName()!= null)
        {
            setRimCoreActCdSets(ce, DCCOMPLETIONCODE);
        }
        else
        {
            this.setDcCompletionCodeEtsId(ce.code().toString());
        }
    }

    public CE getCompletionCode() throws Hl7Exception 
    {
    	if (this.getDcCompletionCodeEtsId().equals(DCCOMPLETIONCODE.toString()))
        {
            for (RimCoreActCdSets actCdSets : this.getActCdSetsList())
            {
                if (this.getDcCompletionCodeEtsId().equals(actCdSets.getAttributeTypeCode()))
                {
                    return DataTypeUtil.getCE(actCdSets);
                }
            }            
        }
        else
            return DataTypeUtil.getCE(this.getDcCompletionCodeEtsId()); 
        return null;
    }
   
    public void setCopyTime(TS copyTime) {
        this.setDcCopyDateTs(new Timestamp(copyTime.toDate().getTime()));
        this.setDcCopyDateTxt(copyTime.literal().toString());
    }

    public TS getCopyTime() throws Hl7Exception {
        return DataTypeUtil.getTS(new Date(this.getDcCopyDateTs().getTime()));
    }
/*
    public void setSetId(II setId) {
        RimCoreActIi actIi = new RimCoreActIi();  
        actIi.setRootId(setId.root().toString());
        actIi.setExtensionTxt(setId.extension().toString());
        actIi.setCreatedBy(this.getCreatedBy());
        actIi.setCreationDate(this.getCreationDate());
        actIi.setLastUpdatedBy(this.getLastUpdatedBy());
        actIi.setLastUpdateDate(this.getLastUpdateDate());
        actIi.setLastUpdateLogin(this.getLastUpdateLogin());         
        this.addActIi(actIi);  
    }

    public II getSetId() {
        return null;
    }
*/    

    public void setStorageCode(CE ce) {
    	if(null != ce && ce.codeSystemEnName()!=null && ce.codeName()!= null)
        {
            setRimCoreActCdSets(ce, DCSTORAGECDSETCODE);
        }
        else
        {
            this.setDcStorageCodeEtsId(ce.code().toString());
        }
    }

    public CE getStorageCode() throws Hl7Exception {
    	if (this.getDcStorageCodeEtsId().equals(DCSTORAGECDSETCODE.toString()))
        {
            for (RimCoreActCdSets actCdSets : this.getActCdSetsList())
            {
                if (this.getDcStorageCodeEtsId().equals(actCdSets.getAttributeTypeCode()))
                {
                    return DataTypeUtil.getCE(actCdSets);
                }
            }            
        }
        else
            return DataTypeUtil.getCE(this.getDcStorageCodeEtsId()); 
        return null;
    }
    
    /**
     * 设置文档的类别
     * @param ce
     */
    public void setCategoryCode(CE ce) {
        if(null != ce && ce.codeSystemEnName()!=null && ce.codeName()!= null)
        {
            setRimCoreActCdSets(ce, DCCATEGORYCDSETCODE);
        }
        else
        {
            this.setDcCategoryCodeEtsId(ce.code().toString());
        }
    }
 
    /**
     * 得到文档类别字典（包含文档的诊疗类别等）
     * @return
     * @throws Hl7Exception
     */
    public CE getCategoryCode() throws Hl7Exception {
        if ( null !=this.getDcCategoryCodeEtsId() && this.getDcCategoryCodeEtsId().equals(DCCATEGORYCDSETCODE.toString()))
        {
            for (RimCoreActCdSets actCdSets : this.getActCdSetsList())
            {
                if (this.getDcCategoryCodeEtsId().equals(actCdSets.getAttributeTypeCode()))
                {
                    return DataTypeUtil.getCE(actCdSets);
                }
            }            
        }
        else
            return DataTypeUtil.getCE(this.getDcCategoryCodeEtsId()); 
        return null;
    }

    public void setVersionNumber(INT versionNumber) {
        this.setDcVersionNum(versionNumber.longValue());
    }

    public INT getVersionNumber() throws Hl7Exception {
        return DataTypeUtil.getINT(this.getDcVersionNum());
    }
    
    /**
     * 设置文档的状态
     * @param medRecordsState ST类型的文档状态编码
     */
    public void setMedRecordsState(ST medRecordsState){
    	this.setMedicalRecordsState(medRecordsState.stringValue());
    }
    /**
     * 得到一个文档状态编码
     * @return ST
     * @throws Hl7Exception
     */
    public ST getMedRecordState() throws Hl7Exception{
    	return DataTypeUtil.getST(this.getMedicalRecordsState());
    }
    
    
    //Document    
        @Column(name = "DC_ASSIGNING_AUTHORITY_NAME")    protected String dcAssigningAuthorityName;
        @Column(name = "DC_COMPLETION_CODE_ETS_ID")    
        protected String dcCompletionCodeEtsId = DCCOMPLETIONCODE.toString();
        @Column(name = "DC_COPY_DATE_TS")    protected Timestamp dcCopyDateTs;
        @Column(name = "DC_COPY_DATE_TXT")    protected String dcCopyDateTxt;
        @Column(name = "DC_DISPLAYABLE_FLAG")    protected String dcDisplayableFlag;
        @Column(name = "DC_EXTENSION_TXT")    protected String dcExtensionTxt;
        @Column(name = "DC_STORAGE_CODE_ETS_ID")
        protected String dcStorageCodeEtsId = DCSTORAGECDSETCODE.toString();
        @Column(name = "DC_VERSION_NUM")    protected Long dcVersionNum;
        @Column(name = "DC_MEDICALRECORDS_STATE")    protected String medicalRecordsState;//病历状态
        @Column(name = "DC_CATEGORY_CODE_ETS_ID")
        protected String dcCategoryCodeEtsId = DCCATEGORYCDSETCODE.toString();
        




	protected String getDcAssigningAuthorityName() {
        return dcAssigningAuthorityName;
    }

    protected void setDcAssigningAuthorityName(String dcAssigningAuthorityName) {
        this.dcAssigningAuthorityName = dcAssigningAuthorityName;
    }

    protected String getDcCompletionCodeEtsId() {
        return dcCompletionCodeEtsId;
    }

    protected void setDcCompletionCodeEtsId(String dcCompletionCodeEtsId) {
        this.dcCompletionCodeEtsId = dcCompletionCodeEtsId;
    }

    protected Timestamp getDcCopyDateTs() {
        return dcCopyDateTs;
    }

    protected void setDcCopyDateTs(Timestamp dcCopyDateTs) {
        this.dcCopyDateTs = dcCopyDateTs;
    }

    protected String getDcCopyDateTxt() {
        return dcCopyDateTxt;
    }

    protected void setDcCopyDateTxt(String dcCopyDateTxt) {
        this.dcCopyDateTxt = dcCopyDateTxt;
    }

    protected String getDcDisplayableFlag() {
        return dcDisplayableFlag;
    }

    protected void setDcDisplayableFlag(String dcDisplayableFlag) {
        this.dcDisplayableFlag = dcDisplayableFlag;
    }

    protected String getDcExtensionTxt() {
        return dcExtensionTxt;
    }

    protected void setDcExtensionTxt(String dcExtensionTxt) {
        this.dcExtensionTxt = dcExtensionTxt;
    }


    protected String getDcStorageCodeEtsId() {
        return dcStorageCodeEtsId;
    }

    protected void setDcStorageCodeEtsId(String dcStorageCodeEtsId) {
        this.dcStorageCodeEtsId = dcStorageCodeEtsId;
    }

    protected Long getDcVersionNum() {
        return dcVersionNum;
    }

    protected void setDcVersionNum(Long dcVersionNum) {
        this.dcVersionNum = dcVersionNum;
    }
    protected String getMedicalRecordsState() {
		return medicalRecordsState;
	}

    protected void setMedicalRecordsState(String medicalRecordsState) {
		this.medicalRecordsState = medicalRecordsState;
	}

    public String getDcCategoryCodeEtsId()
    {
        return dcCategoryCodeEtsId;
    }

    public void setDcCategoryCodeEtsId(String dcCategoryCodeEtsId)
    {
        this.dcCategoryCodeEtsId = dcCategoryCodeEtsId;
    }
    
}
